package com.treasure.mall.biz.entity.product;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.treasure.mall.biz.entity.BasePO;
import com.treasure.mall.biz.entity.user.MerchantPO;
import com.treasure.mall.zbq.common.ienum.auction.AuctionEnums;
import com.treasure.mall.zbq.common.ienum.product.ProductEnums;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/6/11
 */
@Entity
@Table(name = "zb_product")
@Setter
@Getter
@Cacheable(false)
@NoArgsConstructor
public class ProductPO extends BasePO {
    /**
     * 产品id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "product_id")
    private Long productId;

    /**
     * 商家
     */
    @ManyToOne
    @JoinColumn(name = "merchant_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private MerchantPO merchantPO;
    /**
     * 产品名称
     */
    @Column(name = "product_name", length = 400)
    private String productName;

    /**
     * 产品类型
     */
    @Column(name = "product_type")
    private Integer productType;

    /**
     * 销售类型
     * {@link com.treasure.mall.zbq.common.ienum.product.ProductEnums.SaleType}
     */
    @Column(name = "sale_type")
    private ProductEnums.SaleType saleType = ProductEnums.SaleType.NORMAL;
    /**
     * 拍卖状态，拍卖的商品不为空
     */
    @Column(name = "auction_status")
    private AuctionEnums.AuctionStatus auctionStatus = AuctionEnums.AuctionStatus.TO_START;

    @ManyToOne
    @JoinColumn(name = "cat_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private CategoryPO category;

    /**
     * 尺寸
     */
    @Column(name = "prop_size", length = 200)
    private String propSize;

    /**
     * 材质
     */
    @Column(name = "prop_material", length = 200)
    private String propMaterial;

    /**
     * 产地
     */
    @Column(name = "location", length = 200)
    private String location;

    /**
     * 重量单位
     */
    @Column(name = "weight_unit", length = 20)
    private String weightUnit;

    /**
     * weight
     */
    @Column(name = "weight", scale = 3, precision = 9)
    private BigDecimal weight;

    /**
     * 价格
     */
    @Column(name = "price", scale = 2, precision = 10)
    private BigDecimal price;

    /**
     * 市场价
     */
    @Column(name = "mkt_price", scale = 2, precision = 10)
    private BigDecimal mktPrice;

    /**
     * 服务标签
     */
    @Column(name = "service_tags", length = 100)
    private String serviceTags;

    /**
     * 限购数
     */
    @Column(name = "limited_num", length = 10)
    private Integer limitedNum;

    /**
     * 主图
     */
    @Column(name = "main_pic", length = 250)
    private String mainPic;

    /**
     * 详情
     */
    @Column(name = "detail_content", columnDefinition = "longtext")
    private String detailContent;

    /**
     * 视频
     */
    @Column(name = "video", length = 250)
    private String video;

    /**
     * 一级返利百分比
     */
    @Column(name = "rebate_rate_one", scale = 3, precision = 6)
    private BigDecimal rebateRateOne;

    /**
     * 二级返利百分比
     */
    @Column(name = "rebate_rate_two", scale = 3, precision = 6)
    private BigDecimal rebateRateTwo;
    /**
     * 佣金百分比
     */
    @Column(name = "commission_rate", scale = 3, precision = 6)
    private BigDecimal commissionRate;

    /**
     * 状态
     */
    @Column(name = "status_code")
    private Integer statusCode;

    /**
     * 删除标志
     */
    @Column(name = "delete_flag", length = 1)
    private Integer deleteFlag;

    /**
     * 副标题
     */
    @Column(name = "sub_title", length = 400)
    private String subTitle;

    /**
     * 排序号
     */
    @Column(name = "sort_no")
    private Integer sortNo;

    /**
     * 房间号
     */
    @Column(name = "room_id")
    private Long roomId = 0L;
    /**
     * 库存
     */
    @Column(name = "store")
    private int store = 1;
    /**
     * 锁定数
     */
    @Column(name = "freeze")
    private int freeze;
    /**
     * 专属用户id
     */
    @Column(name = "exclusive_userId")
    private Long exclusiveUserId;
    /**
     * 商品标签
     */
    @Column(name = "productTags", length = 150)
    private String productTags;

    /**
     * 是否推荐
     */
    @Column(name = "recommend_flag")
    private Integer recommendFlag;

    /**
     * 乐观锁
     */
    @Column(name = "zb_version")
    private long version;

    public void preInsert() {
        this.setCreateTime(LocalDateTime.now());
        this.setDeleteFlag(0);
    }

    public void preUpdate() {
        this.setUpdateTime(LocalDateTime.now());
    }

    @Transient
    @JsonIgnore
    private List<String> arrPics;

    @Transient
    @JsonIgnore
    private String catName;

    @Transient
    @JsonIgnore
    private String exclusiveUserInfo;

    public int availableStore() {
        return this.store - this.freeze;
    }

    public ProductPO(Long productId) {
        this.productId = productId;
    }
}
